package 网络编程.TCPFILEECHO3;

import 网络编程.TCPFileCopy1.StreamUtil;

import java.io.*;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-05
 * Time: 13:53
 */
public class TCPFILEEchoClint {

    Socket socket = null;

    public TCPFILEEchoClint() throws IOException {
        socket = new Socket("127.0.0.1",8000);
    }

    public void start() throws IOException {
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            //构造一个请求发送qie图片
            String FilePath = "e://qie1.png";
            //读取磁盘文件的输入流
            BufferedInputStream bufferedInputStream = new
                    BufferedInputStream(new FileInputStream(FilePath));
            //将文件读入到字节数组中-->需要先将图片转化成字节数组
            byte[] bytes = StreamUtil.streamByteArray(bufferedInputStream);
            //将字节数组发送给服务端
            BufferedOutputStream bufferedOutputStream =
                    new BufferedOutputStream(outputStream);
            bufferedOutputStream.write(bytes);

            bufferedOutputStream.flush();
            socket.shutdownOutput();//设置结束标记

            String s = StreamUtil.streamToString(inputStream);
            System.out.println(s);
            bufferedOutputStream.close();//关闭流
            bufferedInputStream.close();
        }finally {
            socket.close();
        }
    }

    public static void main(String[] args) throws IOException {
         TCPFILEEchoClint tcpfileEchoClint = new TCPFILEEchoClint();
         tcpfileEchoClint.start();
    }

}
